import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { RecordService } from './record.service';
import { UseLog } from 'src/log/useLog.decorator';
import { CurrentUser } from 'src/user/user.decorator';
import { UserSys } from 'src/user/userSys.entity';
import { UserGuard } from 'src/role/role.guard';
import { DeptRoleEnum, RoleEnum } from 'src/role/role.enum';
import { UserService } from 'src/user/user.service';
import { RecordStatus } from './record.entity';
import { AuthGuard } from '@nestjs/passport';

@Controller('api/record')
@ApiTags("面试相关")
@UseGuards(AuthGuard("jwt"))
export class RecordController {
    constructor(
        private readonly recordService: RecordService,
        private readonly userService: UserService
    ) { }



    @Post(":id/set")
    @UseLog("设置面试记录")
    @UseGuards(UserGuard([RoleEnum.director]))
    set(
        @Param("id") id: number,
        @Body() { entryTime, noticeEntry }
    ) {
        return this.recordService.record.update({
            id
        }, { entryTime, noticeEntry: noticeEntry == "true" })
    }

    @Get(":id/:status/setStatus")
    @UseLog("设置状态")
    @UseGuards(UserGuard([RoleEnum.director]))
    async setStatus(
        @Param('id') id: number,
        @Param("status") status: RecordStatus
    ) {
        const entity = await this.recordService.record.findOne({
            where: {
                id
            },
            relations: ["system", "user"]
        })
        entity.status = status;
        if (status === "已入职") {
            entity.user.system = entity.system;
            await this.userService.userSys.save(entity.user);
        }
        return await this.recordService.record.save(entity);
    }


    @Post("add")
    @UseLog("添加面试信息")
    @UseGuards(UserGuard([DeptRoleEnum.personnel]))
    async add(
        @CurrentUser() user: UserSys,
        @Body() { name, sex, age, education, graduate, professional, educationTime, isGuaduation, nativePlace, address, phone, ...rest }
    ) {
        const account = await this.userService.create({ unique: true, name, sex, age, education, graduate, professional, educationTime, isGuaduation, nativePlace, address, phone })
        const entity = this.recordService.record.create({
            createUser: user,
            user: account,
            system: user.system,
            ...rest
        })
        return await this.recordService.record.save(entity);
    }

    @Post("del")
    @UseLog("删除面试记录")
    @UseGuards(UserGuard([RoleEnum.director]))
    del(
        @Body() { ids }
    ) {
        return this.recordService.record.delete(ids)
    }

    @Get("get")
    @ApiOperation({ summary: "获取记录" })
    @UseGuards(UserGuard([DeptRoleEnum.personnel]))
    async get(
        @CurrentUser() user: UserSys,
        @Query("type") type: string,
        @Query("date") date: string
    ) {
        let query = this.recordService.record
            .createQueryBuilder("record")
            .leftJoinAndSelect("record.system", "system")
            .leftJoinAndSelect("record.createUser", "createUser")
            .leftJoinAndSelect("record.user", "user")
        if (type === "all") {
            query = query.where("record.noticeEntry IS NULL OR record.noticeEntry = :noticeEntry", { noticeEntry: true })
                .andWhere("record.status IS NULL OR record.status = :status", { status: "已入职" });
        } else {
            query = query.where('record.noticeEntry = :noticeEntry OR (record.noticeEntry = :noticeEntryValue AND record.status = :recordStatus)', {
                noticeEntry: false,
                noticeEntryValue: true,
                recordStatus: '未入职',
            })
        }
        if (user.role !== RoleEnum.boos) {
            query = query.andWhere("system.id = :id", { id: user.system.id });
        }
        const data = await query
            .orderBy("record.id", "DESC")
            .getMany()
        return data
    }
}
